package com.bestcem.xm.wallet.grpc.client;

import com.bestcem.bp.pay.grpc.v1.services.ExpenseServiceGrpc;
import com.bestcem.bp.pay.grpc.v1.services.GetExpenseRequest;
import com.bestcem.bp.pay.grpc.v1.services.GetExpenseResponse;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.common.core.sign.grpc.core.GrpcTemplate;
import com.bestcem.xm.wallet.grpc.client.converter.WalletWalletConverter;
import com.bestcem.xm.wallet.grpc.client.dto.ExpenseInfoDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 费用服务相关grpc接口
 *
 * @author panyun <yun.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/20 21:06
 */
@Slf4j
@Service(value = "walletExpenseGrpcService")
public class WalletExpenseGrpcService {

    @GrpcClient(GrpcClientNames.PAY)
    private ExpenseServiceGrpc.ExpenseServiceBlockingStub stub;

    @Resource
    private GrpcTemplate grpcTemplate;

    /**
     * 企业钱包-【消费:[红包发放、短信消费、版本购买]、发票:[未开票、开票中、已开票]】界面金额查询
     *
     * @param walletId 钱包id
     * @return java.lang.Object
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/20 21:01
     */
    public ExpenseInfoDTO getWalletExpenses(String walletId) {
        if (log.isInfoEnabled()) {
            log.info("获取获取费用信息：id:{} ", walletId);
        }
        GetExpenseRequest.Builder builder = GetExpenseRequest.newBuilder().setWalletId(walletId);
        GetExpenseRequest request = builder.build();
        grpcTemplate.addSign(request, GrpcTemplate.SEVER_CODE_PAY);
        GetExpenseResponse response = stub.getExpense(request);
        // 校验查询结果
        if (Code.ERROR_VALUE == response.getStatus().getCodeValue()) {
            log.error("获取获取费用信息失败：id:{}", walletId);
            return null;
        }
        return WalletWalletConverter.responseToExpenseInfoDTO(response);
    }

}
